package com.softeem.mpdemo.controller;

import cn.hutool.core.util.DesensitizedUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.softeem.mpdemo.dto.Result;
import com.softeem.mpdemo.entity.Singer;
import com.softeem.mpdemo.entity.Singer;
import com.softeem.mpdemo.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
public class SingerController {

    @Autowired
    private SingerService singerService;

    @RequestMapping("/singer/update")
    public Result modify(Singer singer){
        if(singerService.updateById(singer)){
            return Result.ok().setMsg("修改成功").setData(singer);
        }
        return Result.fail().setMsg("更新失败");
    }

    @RequestMapping("singer/listAll")
    public Result list(){
        List<Singer> list = singerService.list();
        if(list.isEmpty()){
            return Result.fail().setMsg("无记录");
        }
        return Result.ok().setMsg("查询成功").setData(list);
    }

    @RequestMapping("singer/list")
    public Result list(Integer page,Integer limit) {
        List<Singer> list = singerService.list(new Page<>(page, limit));
        if (list.isEmpty()) {
            return Result.fail().setMsg("无记录");
        }
        return Result.ok().setMsg("查询成功").setData(list);
    }

    @RequestMapping("singer/del")
    public Result delById(Integer id){
        Singer singer = singerService.getById(id);
        if(Objects.isNull(singer)){
            return Result.fail().setMsg("该管理员不存在或者已经被删除!");
        }
        if(singerService.removeById(id)){
            return Result.ok().setMsg("删除成功").setData(singer);
        }
        return Result.fail().setMsg("删除失败");
    }

}